package cn.mapway.tools.db;

import org.nutz.lang.Strings;
import org.nutz.lang.util.Regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTools {
    /**
     * 在字符串 source 中寻找符合 regGroup的字符串
     * 比如 在 testtype:cn.type.Test Name 按照 type:(.*)\\ 查找 获取 cn.type.Test
     *
     * @param regGroup "type:(.*)\\s"
     * @param source
     * @return
     */
    public static final String findInfo(String regGroup, String source) {
        if (Strings.isBlank(regGroup) || Strings.isBlank(source)) {
            return "";
        }
        Pattern pattern = Regex.getPattern(regGroup);
        Matcher matcher = pattern.matcher(source);
        if (matcher.find()) {
            int groupCount= matcher.groupCount();
            System.out.println("groupCount: " + groupCount);
            if (groupCount >= 1) {
                return matcher.group(1);
            } else {
                return "";
            }
        } else {
            return "";
        }
    }
}
